package br.com.passeionaweb.appengine.tabelafipe.data;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class Version {
	public static final String KEY_VERSION = "version";

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private String version;

	@Persistent
	private Long modelid;

	public Version(String version, Long modelid) {
		super();
		this.version = version;
		this.modelid = modelid;
	}

	public Long getModelid() {
		return modelid;
	}

	public void setModelid(Long modelid) {
		this.modelid = modelid;
	}

	public Long getId() {
		return id;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}
	
	public static boolean exists(String version) {
		return getVersion(version) != null;
	}

	public static Version getVersion(String versionname) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Version.class);
		q.setFilter(KEY_VERSION + " == '" + versionname+"'");
		List<Version> models = (List<Version>) q.execute();
		if (models.isEmpty()) {
			return null;
		} else {
			return models.get(0);
		}
	}
}
